package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.dao.SetmealDao;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 32338
 * @date 2024-05-03 19:08:55
 * @description 检查项控制层
 */
@RestController
@RequestMapping("/checkgroup")
@Slf4j
public class CheckGroupController {

    @Autowired
    private CheckGroupService checkGroupService;

    @Autowired
    private SetmealDao setmealDao;



    /**
     * @description
     ***:新增检查组
     **/
    @PostMapping("/add")
    public Result save(@RequestBody CheckGroup checkGroup,
                       @RequestParam List<Integer> checkitemIds){
        log.info("新增检查组");

        checkGroupService.save(checkGroup,checkitemIds);

        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    /**
     * @description
     ***:删除检查组
     **/
    @GetMapping("/deleteById")
    public Result deldeteById( Integer id){
        log.info("删除检查组: {}",id);

        checkGroupService.deleteById(id);

        return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    /**
     * @description
     ***:根据套餐id查询关联的检查项Id
     **/
    @GetMapping("/findCheckGroupIdsBySetmealId")
    public Result findGroupIdBySetmealId( Integer setmealId){
        log.info("根据套餐id查询关联的检查组Id: {}",setmealId);

       List<Integer> checkGroupIds =  setmealDao.findGroupIdBySetmealId(setmealId);

        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkGroupIds);
    }

    /**
     * @description
     ***:根据检查组id查询关联的检查项ids
     **/
    @GetMapping("/findItemIdsByGroupId")
    public Result findItemIdsByGroupId( Integer checkGroupId){
        log.info("根据检查组id查询关联的检查项Id: {}",checkGroupId);

        List<Integer> checkItemIds =  checkGroupService.findItemIdsByGroupId(checkGroupId);

        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemIds);
    }

    /**
     * @description
     ***:修改检查组
     **/
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup,
                       @RequestParam List<Integer> checkitemIds
                       ){
        log.info("修改检查组: {}",checkGroup);

        checkGroupService.edit(checkGroup,checkitemIds);

        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * @description
     ***:分页查询检查组
     **/
    @PostMapping("/findPage")
    public PageResult page(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询检查组:{}",queryPageBean);

        PageResult pageResult = checkGroupService.page(queryPageBean);

        return pageResult;
    }

    /**
     * @description
     ***:查询所有检查组
     **/
    @GetMapping("/findAll")
    public Result findAll(){
        log.info("查询所有检查组");

       List<CheckGroup> checkGroupList =  checkGroupService.findAll();

        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
    }

    /**
     * @description
     ***:根据id查询检查组
     **/
    @GetMapping("/findById")
    public Result findById( Integer id){
        log.info("根据id查询检查组，用于查询回显：{}",id);

        CheckGroup checkGroup = checkGroupService.findById(id);

        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
    }


}
